/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.gzchengguo.cn
* 注意：
* 本软件为www.gzchengguo.cn开发研制，未经购买不得商用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package link.chengguo.orangemall.modules.activity.service.impl;

import cn.hutool.core.util.StrUtil;
import link.chengguo.orangemall.common.service.impl.BaseServiceImpl;
import link.chengguo.orangemall.common.utils.QueryHelpPlus;
import link.chengguo.orangemall.dozer.service.IGenerator;
import link.chengguo.orangemall.modules.activity.domain.OrangeStoreCoupon;
import link.chengguo.orangemall.modules.activity.service.OrangeStoreCouponService;
import link.chengguo.orangemall.modules.activity.service.dto.OrangeStoreCouponDto;
import link.chengguo.orangemall.modules.activity.service.dto.OrangeStoreCouponQueryCriteria;
import link.chengguo.orangemall.modules.activity.service.mapper.OrangeStoreCouponMapper;
import link.chengguo.orangemall.modules.product.domain.OrangeStoreProduct;
import link.chengguo.orangemall.modules.product.service.OrangeStoreProductService;
import link.chengguo.orangemall.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


/**
* @author chengguo
* @date 2020-05-13
*/
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class OrangeStoreCouponServiceImpl extends BaseServiceImpl<OrangeStoreCouponMapper, OrangeStoreCoupon> implements OrangeStoreCouponService {

    private final IGenerator generator;
    private final OrangeStoreProductService storeProductService;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(OrangeStoreCouponQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<OrangeStoreCoupon> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        List<OrangeStoreCouponDto> storeCouponDtos = generator.convert(page.getList(), OrangeStoreCouponDto.class);
        for (OrangeStoreCouponDto storeCouponDto : storeCouponDtos) {
            if(StrUtil.isNotBlank(storeCouponDto.getProductId())){
                List<OrangeStoreProduct> storeProducts = storeProductService.lambdaQuery()
                        .in(OrangeStoreProduct::getId, Arrays.asList(storeCouponDto.getProductId().split(",")))
                        .list();
                storeCouponDto.setProduct(storeProducts);
            }
        }
        map.put("content", storeCouponDtos);
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<OrangeStoreCoupon> queryAll(OrangeStoreCouponQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(OrangeStoreCoupon.class, criteria));
    }


    @Override
    public void download(List<OrangeStoreCouponDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrangeStoreCouponDto orangeStoreCoupon : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("优惠券名称", orangeStoreCoupon.getTitle());
            map.put("兑换消耗积分值", orangeStoreCoupon.getIntegral());
            map.put("兑换的优惠券面值", orangeStoreCoupon.getCouponPrice());
            map.put("最低消费多少金额可用优惠券", orangeStoreCoupon.getUseMinPrice());
            map.put("优惠券有效期限（单位：天）", orangeStoreCoupon.getCouponTime());
            map.put("排序", orangeStoreCoupon.getSort());
            map.put("状态（0：关闭，1：开启）", orangeStoreCoupon.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
